<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="org.lzy.pt.ServerListManager"%>
<%@page import="org.lzy.pt.bean.Server"%>
<%@page import="org.jivesoftware.util.ParamUtils,
	java.net.URLEncoder" errorPage="error.jsp" %>
<%@page import="org.jivesoftware.util.StringUtils" %>

<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %>
<jsp:useBean id="webManager" class="org.jivesoftware.util.WebManager" />
<% webManager.init(request, response, session, application, out ); %>

<%  // Get parameters
    boolean save = ParamUtils.getBooleanParameter(request,"save");
    boolean success = ParamUtils.getBooleanParameter(request,"success");
    String T_DomainName = ParamUtils.getParameter(request,"T_DomainName");
    int T_Port = ParamUtils.getIntParameter(request,"T_Port",0);
    String T_PublicKey = ParamUtils.getParameter(request,"T_PublicKey");
    String T_Pwd = ParamUtils.getParameter(request,"T_Pwd");
    Map<String, String> errors = new HashMap<String, String>();

    // Handle a cancel
    if (request.getParameter("cancel") != null) {
     	response.sendRedirect("server-properties.jsp?T_DomainName="+T_DomainName+"&T_Port="+T_Port);
        return;
    }

    Server server = ServerListManager.getInstance().getServer(T_DomainName, T_Port);

    // Handle a save
    if (save) {
        // If provider requires email, validate
//         if (UserManager.getUserProvider().isEmailRequired()) {
//             if (!StringUtils.isValidEmailAddress(email)) {
//                 errors.put("email","");
//             }
//         }
//         // If provider requires name, validate
//         if (UserManager.getUserProvider().isNameRequired()) {
//             if (name == null || name.equals("")) {
//                 errors.put("name","");
//             }
//         }

        if (errors.size() == 0) {
            server.setT_PublicKey(T_PublicKey);
            server.setT_Pwd(T_Pwd);

            ServerListManager.getInstance().updateServer(server);
            
            response.sendRedirect("server-properties.jsp?editsuccess=true&T_DomainName="+T_DomainName+"&T_Port="+T_Port);
            return;
        }
    }
%>

<html>
    <head>
        <title><fmt:message key="server.edit.form.title"/></title>
        <meta name="subPageID" content="server-properties"/>
    </head>
    <body>
<%  if (!errors.isEmpty()) { %>

    <div class="jive-error">
    <table cellpadding="0" cellspacing="0" border="0">
    <tbody>
        <tr>
            <td class="jive-icon"><img src="images/error-16x16.gif" width="16" height="16" border="0" alt=""/></td>
            <td class="jive-icon-label">

            <% if (errors.get("name") != null) { %>
                <fmt:message key="user.create.invalid_name" />
            <% } else if (errors.get("email") != null) { %>
                <fmt:message key="user.create.invalid_email" />
            <% } %>
            </td>
        </tr>
    </tbody>
    </table>
    </div>
    <br>

<%  } else if (success) { %>

    <div class="jive-success">
    <table cellpadding="0" cellspacing="0" border="0">
    <tbody>
        <tr><td class="jive-icon"><img src="images/success-16x16.gif" width="16" height="16" border="0" alt=""></td>
        <td class="jive-icon-label">
        <fmt:message key="user.edit.form.update" />
        </td></tr>
    </tbody>
    </table>
    </div><br>

<%  } %>

<p>
<fmt:message key="server.edit.form.info" />
</p>

<form action="server-edit-form.jsp">

<%-- <input type="hidden" name="username" value="<%= username %>"> --%>
<input type="hidden" name="save" value="true">

<fieldset>
    <legend><fmt:message key="user.edit.form.property" /></legend>
    <div>
    <table cellpadding="3" cellspacing="0" border="0" width="100%">
    <tbody>
        <tr>
            <td class="c1">
                <fmt:message key="server.T_DomainName" />:
            </td>
            <td>
            	<input type="text" size="30" maxlength="150" name="T_DomainName" value="<%= server.getT_DomainName() %>" readonly="readonly">
            </td>
        </tr>
        <tr>
            <td class="c1">
                <fmt:message key="server.T_Port" />: 
            </td>
            <td>
                <input type="text" size="30" maxlength="150" name="T_Port" value="<%= server.getT_Port() %>" readonly="readonly">
            </td>
        </tr>
<!--         <tr> -->
<!--             <td class="c1"> -->
<%--                 <fmt:message key="server.T_PublicKey" />:  --%>
<!--             </td> -->
<!--             <td> -->
<!--                 <input type="text" size="30" maxlength="150" name="T_PublicKey" -->
<%--                  value="<%= ((server.getT_PublicKey()!=null) ? server.getT_PublicKey() : "") %>"/> --%>
<!--             </td> -->
<!--         </tr> -->
         <tr>
            <td class="c1">
                <fmt:message key="server.T_Pwd" />: 
            </td>
            <td>
                 <input type="text" size="30" maxlength="150" name="T_Pwd"
                 value="<%= ((server.getT_Pwd()!=null) ? server.getT_Pwd() : "") %>">
            </td>
        </tr>
    </tbody>
    </table>
    </div>

</fieldset>

<br><br>

<input type="submit" value="<fmt:message key="global.save_properties" />">
<input type="submit" name="cancel" value="<fmt:message key="global.cancel" />">

</form>

<br/>

<span class="jive-description">
* <fmt:message key="server.create.requied" />
</span>

    </body>
</html>